// shopPages/renmairadar/renmairadar.js
var page = 1;
var newsList = [];
var selectedNews = false;
import * as echarts from '../ec-canvas/echarts';
import time_change from "../../utils/util.js";

const app = getApp();

const G = app.globalData.ENUM_LOG;

var logType = G.M_VIST
var dateType = 'TODAY'
var canvastest = '';
var widthtest = '';
var heighttest = '';
var startDay = '';
var endDay = '';

function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });

  canvastest = canvas;
  widthtest = width;
  heighttest = height;
  canvas.setChart(chart);
  var dataX = [0]
  var dataY = [0]
  wx.request({
    url: app.globalData.bossradar + '/api/1.0/getChartData',
    data: {
      logType: logType,
      operationId: app.globalData.userInfo.id,
      dateType: dateType,
      startDay: startDay,
      endDay: endDay
    },
    method: 'POST',
    header: {
      'content-type': 'application/json' // 默认值
    },
    success(res) {
    //  console.log(res.data)
      // dataY = res.data
      for (var i = 0; i < res.data.data.length; i++) {
        dataX.push(res.data.data[i].date)
        dataY.push(res.data.data[i].count)
      }

      var option = {
        color: ["#37A2DA"],
        legend: {
          data: ['图例'],
          top: 10,
          left: 'center',
          "textStyle": {
            "fontSize": 20
          },
          // backgroundColor: 'red',
          z: 200
        },
        grid: {
          containLabel: true
        },
        tooltip: {
          show: true,
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          //   data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],

          data: dataX,
          // show: false
        },
        yAxis: {
          x: 'center',
          type: 'value',
          splitLine: {
            lineStyle: {
              type: 'dashed'
            }
          }
          // show: false
        },
        series: [{
          name: '图例',
          type: 'line',
          "textStyle": {
            "fontSize": 20
          },
          smooth: true,
          // data: [18, 36, 65, 30, 78, 40, 33]
          data: dataY
        }]
      };
      chart.setOption(option);
      return chart;

    }
  })



}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: ['今日', '本周', '本月', '近三月'],
    curren: 0,
    taskBack: false,
    tabnum: [{
        //   num: 16514,
        name: '浏览'
      },
      {
        //  num: 16514,
        name: '加入购物车'
      },
      {
        //   num: 16514,
        name: '下单'
      },
      {
        //  num: 16514,
        name: '分享'
      }
    ],
    curren1: 0,
    list: [],
    ec: {
      onInit: initChart
    },
    contentList: [],
  },

  bindtab(e) {
    newsList = []
    page = 1
    var index = e.currentTarget.id;
    this.setData({
      curren: index,
      taskBack: false
    })

    if (index == 0) {
      dateType = 'TODAY'
    } else if (index == 1) {
      dateType = 'WEEK'
    } else if (index == 2) {
      dateType = 'MONTH'
    } else if (index == 3) {
      dateType = 'THREE_MONTH'
    } else if (index == 4) {
      dateType = 'SELECT_DAY'

      startDay = '';
      endDay = '';

    }
    initChart(canvastest, widthtest, heighttest);
    this.getnewdata()

  },
  bindtab1(e) {
    newsList = []
    page = 1
    var index = e.currentTarget.id;
    this.setData({
      curren1: index
    })
    if (index == 0) {
      logType = G.M_VIST
    } else if (index == 1) {
      logType = G.M_ADDCART
    } else if (index == 2) {
      logType = G.M_BUY
    } else if (index == 3) {
      logType = G.M_SHARE
    }
    initChart(canvastest, widthtest, heighttest);
    this.getnewdata()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    dateType = 'TODAY'
    logType = G.M_VIST
    var day3 = new Date();
    day3.setTime(day3.getTime());
    var month = day3.getMonth() + 1;
    if (month.toString().length == 1) {
      month = '0' + month;
    }
    var day = day3.getDate();
    if (day.toString().length == 1) {
      day = '0' + day;
    }
    var start = day3.getFullYear() + "-" + month + "-01";
    var end = day3.getFullYear() + "-" + month + "-" + day;
    that.setData({
      start: start,
      end: end
    });
    page = 1
    selectedNews = false;
    newsList = []
    that.getnewdata()
  },
  getnewdata() {
    var that = this
    wx.request({
      url: app.globalData.bossradar + '/api/1.0/getLogListByLogTypeAndUid',
      data: {
        page: page,
        pageSize: 10,
        logType: logType,
        uid: app.globalData.userInfo.id,
        dateType: dateType,
        startDay: startDay,
        endDay: endDay
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success(res) {
        console.log(res.data)

        var contentList = [];
        var timelist = [];
        for (var i = 0; i < res.data.data.list.length; i++) {
          var time = time_change.imestampToTime(res.data.data.list[i].updateTime)
          res.data.data.list[i].show_time = time;
          switch (res.data.data.list[i].logType) {
            case 1:
              res.data.data.list[i].log_desc =  '点赞了你的人脉';
              break;
            case 2:
              res.data.data.list[i].log_desc =  '收藏了你的人脉';
              break;
            case 30:
              res.data.data.list[i].log_desc =  '保存了你的人脉海报';
              break;
            case 3:
              res.data.data.list[i].log_desc =  '分享了你的人脉';
              break;
            case 5:
              res.data.data.list[i].log_desc =  '浏览了你的人脉';
              break;
            case 13:
              if (res.data.data.list[i].goodName == '') {
                res.data.data.list[i].log_desc =  '浏览了你的商城';
              } else {
                res.data.data.list[i].log_desc =  '浏览了你的' + res.data.data.list[i].goodName;
              }
              break;
            case 31:
              res.data.data.list[i].log_desc =  '把商品' + res.data.data.list[i].goodName + '加入购物车';
              break;
            case 18:
              res.data.data.list[i].log_desc =  '下单了你的' + res.data.data.list[i].goodName + '商品';
              break;
            case 32:
              res.data.data.list[i].log_desc =  '分享了你的' + res.data.data.list[i].goodName + '商品';
              break;
            case 33:
              res.data.data.list[i].log_desc =  '保存了你的' + res.data.data.list[i].goodName + '商品图片';
              break;
            case 6:
              res.data.data.list[i].log_desc =  '打开了聊天页面';
              break;
            case 7:
              res.data.data.list[i].log_desc =  '访问了你的官网';
              break;
            case 9:
              res.data.data.list[i].log_desc =  '播放了你的视频介绍';
              break;
            case 10:
              res.data.data.list[i].log_desc =  '播放了你的语音介绍';
              break;
            case 14:
              res.data.data.list[i].log_desc =  '点击了购买名片';
              break;
            case 34:
              res.data.data.list[i].log_desc =  '购买了名片';
              break;
            case 35:
              res.data.data.list[i].log_desc =  '购买名片成功';
              break;

            case 17:
              res.data.data.list[i].log_desc =  '添加了收货地址';
              break;
            case 18:
              res.data.data.list[i].log_desc =  '提交订单';
              break;
            case 19:
              res.data.data.list[i].log_desc =  '商品付款成功';
              break;
            case 20:
              res.data.data.list[i].log_desc =  '取消订单';
              break;
            case 21:
              res.data.data.list[i].log_desc =  '动态点赞';
              break;
            default:
              break;
          }
          newsList.push(res.data.data.list[i]);
        }

        if (res.data.data.list.length < 10) {
          selectedNews = false;
        } else {
          selectedNews = true;
          page += 1;
        }
        that.setData({
          list: newsList,
        });
        wx.hideLoading();
      },
      fail: function(res) {
        console.log("失败")
      },
      complete: function(res) {},
    })
  },
  bindDateChange: function(e) {
    this.setData({
      start: e.detail.value
    })
  },
  bindDateChange1: function(e) {
    this.setData({
      end: e.detail.value
    })
  },
  isShow() {
    var taskBack = this.data.taskBack
    this.setData({
      taskBack: true
    })
  },
  getData() {
    var that = this
    var taskBack = this.data.taskBack
    this.setData({
      taskBack: false
    })
    newsList = []
    page = 1
    dateType = 'SELECT_DAY'
    startDay = that.data.start
    endDay = that.data.end
    initChart(canvastest, widthtest, heighttest);
    that.getnewdata()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (selectedNews == true) {
      var that = this;
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1  
      that.getnewdata();

    } else {
      wx.hideLoading();
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // },
  customerDetail: function(e) {
    wx.navigateTo({
      url: '/radarPages/Customerxq/Customerxq?mid=' + e.currentTarget.dataset.operationid,
    })
  },

  //获取商场雷达列表数据
  getDataList: function() {
    var that = this;
    wx.request({
      url: app.globalData.bossradar +'/api/1.0/getLogListByLogTypeAndUid',
      data: {
        uid: app.globalData.userInfo.id, //用户id
        dateType: 'ALL_DAY',
        page: 1,
        pageSize: 20,
        logType: '',
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
      }
    })
  },
  //获取折线图数据
  getChartList: function() {
    var that = this;
    wx.request({
      url: app.globalData.bossradar +'/api/1.0/getChartData',
      data: {
        uid: app.globalData.userInfo.id, //用户id
        dateType: 'ALL_DAY',
        page: 1,
        pageSize: 20,
        logType: '',
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
      }
    })
  }

})